package com.junyidao.game.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.junyidao.game.mapper.TGamesMapper;
import com.junyidao.game.pojo.TGames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zzh on 2016/9/3.
 * 文件描述 ：
 */
@Service
@Transactional
public class GameService {

    @Autowired
    public TGamesMapper gamesMapper;

    @Autowired
    private SequenceService sequenceService;

    public Map<String, Object> getNewGame(TGames games) {
        PageHelper.startPage(games.getPage(), games.getRows());
        List<TGames> list = gamesMapper.getNewGame(games);
        PageInfo page = new PageInfo(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", page.getTotal());
        return map;
    }

    public Map<String, Object> getPopularGame(TGames games) {
        PageHelper.startPage(games.getPage(), games.getRows());
        List<TGames> list = gamesMapper.getPopularGame(games);
        PageInfo page = new PageInfo(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", page.getTotal());
        return map;
    }

    public Map<String, Object> getServiceGame(TGames games) {
        PageHelper.startPage(games.getPage(), games.getRows());
        List<TGames> list = gamesMapper.getServiceGame(games);
        PageInfo page = new PageInfo(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", page.getTotal());
        return map;
    }

    public Map<String, Object> getPageMap(List<Object> list) {
        PageInfo page = new PageInfo(list);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("count", page.getTotal());
        return map;
    }

    public TGames getGameById(Integer gameId) {
        TGames par = new TGames();
        par.setGameId(gameId);
        TGames game = gamesMapper.selectOne(par);
        return game;
    }

    public void addGame() throws Exception {
        Integer id = sequenceService.getNextSequence("GAME_ID");
        TGames games = new TGames();
        games.setGameId(id);
        games.setGameLoadflag("1");
        games.setGameName("testGame");
        games.setGamePath("www.baidu.com");
        FileInputStream in = new FileInputStream(new File("E:\\junyidao\\logo.jpg"));
        int i = in.available();
        byte[] data = new byte[i];
        in.read(data);
        in.close();
        games.setGameLogo(data);
        gamesMapper.insert(games);

    }

}
